<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/17
 * Time: 上午8:51
 */

namespace app\doctor\controller;
use app\common\model\Base;
use app\common\model\PatientCaseType;
use PHPSTORM_META\elementType;
use think\Db;

/**
 * 首页
 *
 */
class Index extends Basecontroller
{
    protected $noNeedLogin = [];

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Patient');

    }

    /**
     * 首页
     */
    public function index()
    {

        //获取搜索出来的患者列表
        \think\Loader::action('Patient/index','event');

//        var_dump($this->doctor);
//        var_dump( $this->doctor->department);
        return $this->fetch();

    }

    /**
     * 医生账号设置
     */
    public function edit_doctor()
    {

        if ($this->request->isPost()) {
            $form_data =$this->request->param();

            if (!empty($form_data['password'])  && !empty($form_data['newpassword']) && !empty($form_data['oldpassword'])) {
                if ($form_data['password'] != $form_data['newpassword']) {
                    $this->error("两次密码不一致!");
                }
                $form_data['password']  = md5($form_data['password'] );
            } else if ($form_data['password'] != $form_data['newpassword']) {
                $this->error("两次密码不一致!");
            } else if (empty($form_data['password']) && empty($form_data['newpassword']) && empty($form_data['oldpassword'])) {
                $res = Db::name('doctor')->where('id', $this->doctor->id)->field('password')->find();
                $form_data['password'] = $res['password'];
            } else {
                $this->error('请正确填写信息!');
            }


            $form_data['id'] = $this->doctor->id;

            $old_data = Base::getOneOldDataByTables($form_data['id'],model('doctor')->base_arr);
            Base::operaTable( model('doctor'), $this->doctor->id, 'edit', $form_data, '', $old_data, model('doctor')->attach_arr, 'id', 2);
            $this->success('编辑成功');

        }
      $case_type_list= $this->getCase_type(array('is_deleted'=>1));
      $this->assign('case_type_list',$case_type_list);
        return $this->fetch();

     }

    /**
     * 新增检查项
     */
    public function add_type()
    {
        if ($this->request->isAjax()) {
            $form_data =$this->request->param();
            $form_data['doctor_id'] = $this->doctor->id;

             $attach_arr = [
                [
                    'table' => 'doctor',
                    'field' => 'name',
                    'primary_key' => 'id',
                    'base_table_field' => 'doctor_id'
                ],



            ];

            $result = Base::operaTable( model('patient_case_type'), $this->doctor->id, 'add', $form_data, '', [],$attach_arr, 'id', 2);
            $this->success('添加成功','');

        }
   }

    /**
     * 删除检查项
     */
    public function del() {
        if ($this->request->isAjax()) {

            $form_data = $this->request->param();

            $result = PatientCaseType::getPatientCaseTypeAdminLog($list_action = '', $form_data,  $this->doctor->id, 2);
//            $this->success("删除成功!",'');
            return $result;
        }

    }


}
